package cn.edu.xmu.restfuldemo.model;

import cn.edu.xmu.restfuldemo.model.Product;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品规格视图对象
 * @author Ming Qiu
 **/
@Data
@ApiModel(description = "商品规格视图对象")
public class ProductRetVo {

    @ApiModelProperty(value = "规格id")
    private Integer id;

    @ApiModelProperty(value = "skuSn")
    private String skuSn;

    @ApiModelProperty(value = "商品名称")
    private String name;

    @ApiModelProperty(value = "商品详情")
    private String detail;

    @ApiModelProperty(value = "商品图片链接")
    private String imageUrl;

    @ApiModelProperty(value = "商品原价")
    private Integer originalPrice;

    @ApiModelProperty(value = "商品价格")
    private Integer price;

    @ApiModelProperty(value = "商品库存")
    private Integer inventory;

    @ApiModelProperty(value = "商品状态")
    private Integer state;

    @ApiModelProperty(value = "商品重量")
    private Integer weight;

    @ApiModelProperty(value = "创建时间")
    private LocalDateTime gmtCreate;

    @ApiModelProperty(value = "修改时间")
    private LocalDateTime gmtModified;

    @ApiModelProperty(value = "商品")
    private List<GoodsRetVo> goods;

    /**
     * 由Vo对象创建Product对象
     * @return Product对象
     */
    public ProductRetVo(Product product){
        this.id = product.getId();
        this.name = product.getName();
        this.skuSn = "";
        this.detail = product.getDetail();
        this.imageUrl = product.getImageUrl();
        this.originalPrice = product.getOriginalPrice();
        this.price = 0;
        this.inventory = 0;
        this.state = product.getState();
        this.weight = product.getWeight();
        this.gmtCreate = product.getGmtCreate();
        this.gmtModified = product.getGmtModified();
        if (null != product.getGoodsList()) {
            List<GoodsRetVo> goodsList = new ArrayList<>(product.getGoodsList().size());

            for (Goods goods : product.getGoodsList()) {
                GoodsRetVo goodsVo = new GoodsRetVo(goods);
                goodsList.add(goodsVo);
            }
            this.goods = goodsList;
        }
    }
}
